import cv2
import numpy as np

from Config import img_path
#腐蚀
cv2.namedWindow("binary",cv2.WINDOW_FREERATIO)
img = cv2.imread('F:\\4908523_03.jpg')
img2 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.shape)
#二值化
ret, binary = cv2.threshold(img2,150,255,cv2.THRESH_BINARY)
#查找轮廓
countours,hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

i = int(0)
for contour in countours:
    area = cv2.contourArea(contour)
    if 3000000 < area < 6000000: #2380000
        break


print(countours[i])


r = cv2.minAreaRect(countours[i])
box = cv2.boxPoints(r)
box = np.intp(box)


#cv2.drawContours(img,[box],0,(0,0,255),5)
cv2.drawContours(img,countours,-1,(0,0,255),5)
#计算面积
area = cv2.contourArea(countours[i])
print("area=%d"%area)

len = cv2.arcLength(countours[i],True)
print("len=%d"%len)

cv2.imshow('binary',img)
cv2.waitKey(0)